﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.Controls
{
    public partial class SupplierListCtrl : BaseListControl, IListControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
        }

        public Suppliers SupplierList
        {
            get { return (Suppliers)SourceObject; }
            set { SourceObject = value; }
        }



        #region IListControl Members

        public void DeleteCheckedItems()
        {
            String checkedItemID = "";
            for (int i = 0; i < grid.Items.Count; i++)
            {
                DataGridItem item = grid.Items[i];
                Control ctrl = item.FindControl("cbSelect");
                if (ctrl != null && ctrl is CheckBox && ((CheckBox)ctrl).Checked && ((CheckBox)ctrl).Attributes["ItemID"] != null)
                    checkedItemID += ((CheckBox)ctrl).Attributes["ItemID"].ToString() + ",";
            }
            if (checkedItemID != "")
                checkedItemID = checkedItemID.Remove(checkedItemID.Length - 1);
            checkedItemID = "(" + checkedItemID + ")";

            String oldFilter = Filter;
            if (!String.IsNullOrEmpty(oldFilter))
                Filter = Filter + " and (" + Suppliers.ColumnNames.ID + " in " + checkedItemID + ")";
            else
                Filter = Suppliers.ColumnNames.ID + " in " + checkedItemID;

            SupplierList.Rewind();
            for (int i = 0; i < SupplierList.RowCount; i++)
            {
                SupplierList.Deleted = true;
                SupplierList.LastModifiedByUser = SessionUser.ID;
                SupplierList.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                SupplierList.MoveNext();
            }

            if (!SaveOnCommit)
                SupplierList.Save();

            Filter = oldFilter;
        }

        protected override void grid_OnItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Control ctrl = e.Item.FindControl("lblBalance");
                if (ctrl != null)
                {
                    int id = (int)grid.DataKeys[e.Item.ItemIndex];
                    Label lbl = ctrl as Label;
                    string oldFilter = SupplierList.Filter;
                    SupplierList.Filter = Customers.ColumnNames.ID + "=" + id;

                    lbl.Text = string.Format("{0:C}", Math.Round(SupplierList.TotalAmount - SupplierList.PaidAmount, 2));
                    SupplierList.Filter = oldFilter;
                }
            }
        }

        public void DeleteSelectedItem()
        {
            throw new NotImplementedException();
        }

        public void AddNewItem(MyGeneration.dOOdads.SqlClientEntity newItem)
        {
            throw new NotImplementedException();
        }

        public void UpdateItem(MyGeneration.dOOdads.SqlClientEntity updatedItem)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}